package com.yy.logic.impl;

import com.yy.logic.IJobLogic;

import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.yy.dao.IJobDAO;
import com.yy.dto.CompanyDTO;
import com.yy.dto.JobDTO;
import com.yy.model.TbCompany;
import com.yy.model.TbJob;
@Component
public class JobLogic implements IJobLogic{

	@Resource
	private IJobDAO jobDAO;
	
	@Transactional
	@Override
	public JSONObject getAllJob(Integer jobcompany) throws Exception {
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		int count=0;
		try{
			List<TbJob> listjobBean=this.jobDAO.getAllJob(jobcompany);
			count=this.jobDAO.getJobCount(jobcompany);
			 System.out.println("this is Job logic******************"+count);
			List<JobDTO> listjobDTO = new ArrayList<JobDTO>();
			Iterator<TbJob> itrjobBean = listjobBean.iterator();
			while(itrjobBean.hasNext()){
				TbJob tbjobBean=(TbJob)itrjobBean.next();
				listjobDTO.add(tbjobBean.toDTO());
			}
			jsonMap.put("data",listjobDTO);
			jsonMap.put("num", count);
		}catch(Exception e){
			e.printStackTrace();
		}
		return JSONObject.fromObject(jsonMap);
	}
	
	@Transactional
	@Override
	public int changeJob(int jobId, String jobname, int jobnum,
			int jobhave) throws Exception {
		// TODO Auto-generated method stub
		int result=0;
		try{
			result =this.jobDAO.changeJob(jobId, jobname, jobnum, jobhave);
		}
		catch(Exception e){
			e.printStackTrace();
			result=0;;
		}
		return result;
	}
	
	@Transactional
	@Override
	public int delJob(int jobId) throws Exception {
		// TODO Auto-generated method stub
		int result=0;
		try{
			result =this.jobDAO.delJob(jobId);
		}
		catch(Exception e){
			e.printStackTrace();
			result=0;;
		}
		return result;
	}
	
	@Transactional
	@Override
	public int addJob(JobDTO tbjob) throws Exception {
		// TODO Auto-generated method stub
		int result=0;
		try{
			result =this.jobDAO.addJob(tbjob.toDAO());
		}
		catch(Exception e){
			e.printStackTrace();
			result=0;;
		}
		return result;
	}
	
}
